﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Admx_Generator
{
    internal partial class FrmSelectSupportedOn : Form
    {
        private SupportedOnList _supportedOnElements;

        internal FrmSelectSupportedOn()
        {
            InitializeComponent();

            this.SelectedSupportedOnItem = null;
            this.txtBxAdmxFile.Text = @"C:\Windows\PolicyDefinitions\Windows.admx";
        }

        #region (Properties)

        /// <summary>
        /// Gets or Sets the seleced SupportedOn item.
        /// </summary>
        internal SupportedOnItem SelectedSupportedOnItem { get; private set; }

        #endregion (Properties)

        #region (Methods)

        /// <summary>
        /// Display the list of SupportedOn elements define in an Admx File
        /// </summary>
        /// <param name="admxFile">ADMX File to extract SupportedOn elements from</param>
        private void DisplaySupportedOn(string admxFile)
        {
            this.lstBxSupportedOnElements.Items.Clear();
            try
            {
                if (System.IO.File.Exists(admxFile))
                {
                    this._supportedOnElements = new SupportedOnList(admxFile);
                    this.lstBxSupportedOnElements.Items.AddRange(this._supportedOnElements.SupportedonItems.ToArray());
                }
            }
            catch (Exception) { }
            this.ValidateData();
        }

        /// <summary>
        /// Check user entries and allow or disallow the 'Ok' button accordingly
        /// </summary>
        private void ValidateData()
        {
            this.btnOk.Enabled = this.lstBxSupportedOnElements.SelectedIndex != -1 && this.lstBxSupportedOnElements.SelectedItem != null;
        }

        #endregion (Methods)

        #region (UI Management)

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileBrowser = new OpenFileDialog();

            fileBrowser.Filter = "*.Admx|*.Admx";
            fileBrowser.ReadOnlyChecked = true;

            if (!String.IsNullOrEmpty(this.txtBxAdmxFile.Text))
            {
                try
                {
                    System.IO.FileInfo presetFile = new System.IO.FileInfo(this.txtBxAdmxFile.Text);
                    if (presetFile.Exists)
                    {
                        fileBrowser.InitialDirectory = presetFile.DirectoryName;
                        fileBrowser.FileName = presetFile.Name;
                    }
                }
                catch (Exception) { }
            }

            if (fileBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txtBxAdmxFile.Text = fileBrowser.FileName;
            }
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            this.txtBxAdmxFile.Text = @"C:\Windows\PolicyDefinitions\Windows.admx";
        }

        private void lstBxSupportedOnElements_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ValidateData();
        }

        private void lstBxSupportedOnElements_DoubleClick(object sender, EventArgs e)
        {
            this.ValidateData();
            if (this.lstBxSupportedOnElements.SelectedIndex != -1 && this.lstBxSupportedOnElements.SelectedItem != null)
                this.btnOk.PerformClick();
        }

        private void txtBxAdmxFile_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.txtBxAdmxFile.Text) && this.txtBxAdmxFile.Text.ToLower().EndsWith(".admx"))
            {
                try
                {
                    System.IO.FileInfo presetFile = new System.IO.FileInfo(this.txtBxAdmxFile.Text);
                    if (presetFile.Exists)
                    {
                        this.DisplaySupportedOn(presetFile.FullName);
                    }
                }
                catch (Exception) { }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.SelectedSupportedOnItem = (SupportedOnItem)this.lstBxSupportedOnElements.SelectedItem;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        #endregion (UI Management)
    }
}
